package cn.edu.swu.miniwebsvr.handler;

import cn.edu.swu.miniwebsvr.core.Handler;
import cn.edu.swu.miniwebsvr.core.Parameter;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class TimeHandler implements Handler {
    @Override
    public void handle(Parameter request, OutputStream response) {
        PrintWriter writer = new PrintWriter(response);

        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String content = "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "<meta charset=\"UTF-8\">\n" +
                "<title>服务器时间 - MiniWebSvr</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<h1>MiniWebSvr Current Time</h1>\n" +
                "<p>Server Time: <strong>" + currentTime + "</strong></p>\n" +
                "<p>这是一个由 MiniWebSvr 提供的时间服务</p>\n" +
                "<p>[222024321072041/王耀东]-服务器时间\n" +
                "</body>\n" +
                "</html>";

        // [222024321072041/王耀东] 构造完整的 HTTP 响应头和响应体
        writer.println("HTTP/1.1 200 OK");
        writer.println("Content-Type: text/html; charset=UTF-8");
        writer.println("Content-Length: " + content.getBytes().length);
        writer.println("Connection: close");
        writer.println("Server: MiniWebSvr/1.0");
        writer.println("Cache-Control: no-cache, no-store, must-revalidate");
        writer.println("Pragma: no-cache");
        writer.println("Expires: 0");
        writer.println(); // 空行分隔头部和正文

        writer.println(content);
        writer.flush();
    }
}
